package com.rongxue.controller.Consult;

import com.rongxue.mapper.SMessagePersonalMapper;
import com.rongxue.pojo.*;
import com.rongxue.service.ConsultService;
import com.rongxue.service.Impl.CashInAndOutServiceImpl;
import com.rongxue.service.Impl.ProjectServiceImpl;
import com.rongxue.utils.CommonUtils;
import com.rongxue.utils.Page;
import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by yangweijian on 2018/1/22.
 */
@Controller
@RequestMapping("/Message")
public class MessageController {

    @Autowired
    private ConsultService consultService;
    
    @Autowired
    private SMessagePersonalMapper sMessagePersonalMapper;

    /**
     * 意见反馈
     * @param xmlBean
     * @return
     */
    @RequestMapping(value = "/feedback", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public ObjectResult feedback(XMLBean xmlBean){
        Map<String,Object> map = new HashMap<String,Object>();
        try {
            if(xmlBean.getToken()==null||xmlBean.getToken().equals(""))  return ObjectResult.build("0","token不存在");
            if(xmlBean.getContent()==null||xmlBean.getContent().trim().equals(""))  return ObjectResult.build("0","反馈内容不能为空");
            String userId = CommonUtils.decToken(xmlBean.getToken());
            if(userId==null||userId.equals("")) return ObjectResult.build("0","用户Id为空");
            map.put("userId",userId);
            map.put("mobile",xmlBean.getMobile());
            map.put("content",xmlBean.getContent());
            map.put("type",xmlBean.getType());
            consultService.insertSuggest(map);
        }
        catch (Exception e){
            e.printStackTrace();
            return ObjectResult.build("0","失败");
        }
        return ObjectResult.build("1","反馈成功");
    }

    /**
     * 个人消息
     * @param xmlBean
     * @return
     */
    @RequestMapping(value = "/personal" , method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public ObjectResult personal(XMLBean xmlBean){
        Map<String,Object> data = new HashMap<String,Object>();
        if(xmlBean.getToken()==null||xmlBean.getToken().equals(""))  return ObjectResult.build("0","token不存在");
        String userId = CommonUtils.decToken(xmlBean.getToken());
        List<MessagePersonal> list = consultService.selectMessagePersonalByUserId(userId);
        Page<MessagePersonal> page = new Page<MessagePersonal>();
        page.setP(xmlBean.getP());
        page.setPageSize(10);
        List<MessagePersonal> content = page.getPageContent(list);
        for (MessagePersonal m:content){
            m.setAdd_time(m.getAdd_time().substring(0,m.getAdd_time().lastIndexOf(".")));
        }
        data.put("list",content);
        data.put("count",page.getPageTotal());
        return ObjectResult.build("1","成功",data);
    }

    /**
     * 系统消息
     * @param xmlBean
     * @return
     */
    @RequestMapping(value = "/get_list", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public ObjectResult messages(XMLBean xmlBean){
        Map<String,Object> data = new HashMap<String,Object>();
        List<Message> list = consultService.selectMessageList();
        Page<Message> page = new Page<>();
        page.setP(xmlBean.getP());
        page.setPageSize(10);
        List<Message> content = page.getPageContent(list);
        for(Message message:content){
            message.setUrl(CashInAndOutServiceImpl.PAGE_NOTIFY_URL+"Message/get_detail?id="+message.getId());
            message.setAdd_time(message.getAdd_time().substring(0,message.getAdd_time().indexOf(".")));
        }

        List<Message> aaa = new ArrayList<>();

        data.put("list",aaa);
        data.put("count",page.getPageTotal());
        return ObjectResult.build("1","成功",data);
    }

    /**
     *最新公告
     * @param xmlBean
     * @return
     */
    @RequestMapping(value = "/get_last", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public ObjectResult get_last(XMLBean xmlBean){
        Message message = sMessagePersonalMapper.selectMessageLast();
        if(message==null) {
            message  = new Message();
            message.setTitle("暂无公告");
        }
        return ObjectResult.build("1","成功",message);
    }

    /**
     * 系统消息详情
     * @param xmlBean
     * @return
     */
    @RequestMapping(value = "/get_detail")
    @ResponseBody
    public ModelAndView message_detail(XMLBean xmlBean){
        String detail = sMessagePersonalMapper.selectMessageDetail(Integer.parseInt(xmlBean.getId()));
        System.out.println(detail);
        ModelAndView modelAndView = new ModelAndView("msg");
        modelAndView.addObject("msg", StringEscapeUtils.unescapeHtml4(detail));
        return modelAndView;
    }

    /**
     * 常见问题
     * @return
     */
//    @RequestMapping(value = "/issue")
//    @ResponseBody
//    public ObjectResult issue(){
//        List<Question> data = consultService.selectQestionList();
//        return ObjectResult.build("1","成功",data);
//    }
    @RequestMapping(value = "/commonProblem")
    @ResponseBody
    public ModelAndView commonProblem(){
        List<Question> list = consultService.selectQestionList();
        StringBuilder sb = new StringBuilder();
        for (Question question :list){
            sb.append("<h3><font face=\"verdana\">");
            sb.append(question.getContent());
            sb.append("</font></h3>");
            sb.append("<p>");
            sb.append(question.getAnswer());
            sb.append("</p>");
        }

        ModelAndView modelAndView = new ModelAndView("msg");
        modelAndView.addObject("msg",StringEscapeUtils.unescapeHtml4(sb.toString()));
        return modelAndView;
    }
    /**
     * 单条消息已读
     * @param xmlBean
     * @return
     */
    @RequestMapping(value = "/read_one", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public ObjectResult read_one(XMLBean xmlBean){
        if(xmlBean.getToken()==null||xmlBean.getToken().equals(""))  return ObjectResult.build("0","token不存在");
        String userId = CommonUtils.decToken(xmlBean.getToken());
        try {
            consultService.updateIsReadByid(xmlBean.getId());
        }catch (Exception e){
            e.printStackTrace();
            return ObjectResult.build("0","失败");
        }
        return ObjectResult.build("1","成功");
    }

    /**
     * 个人信息未读条数
     * @param xmlBean
     * @return
     */
    @RequestMapping(value = "/read_new", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public ObjectResult read_new(XMLBean xmlBean){
        if(xmlBean.getToken()==null||xmlBean.getToken().equals(""))  return ObjectResult.build("0","token不存在");
        String userId = CommonUtils.decToken(xmlBean.getToken());
        Integer row = sMessagePersonalMapper.selectMessageUnreadCountByUserId(userId);
        return ObjectResult.build("0","成功",row);
    }

}
